﻿using ShopFine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShopFine.userControls
{
    public partial class MenuHorizonal : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategories();
            }
        }

        private void LoadCategories()
        {
            var cateList = new CategoryDAO().GetAll();
            rptCates.DataSource = cateList;
            rptCates.DataBind();
        }

        private void LoadSubCategory(int categoryID)
        {

        }

        protected void rptCates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Category cate = e.Item.DataItem as Category;
                HyperLink hplCateLink = e.Item.FindControl("hplCateLink") as HyperLink;
                if (cate != null && hplCateLink != null)
                {
                    hplCateLink.Text = cate.SubCategories.Count <= 0 ? cate.Name : cate.Name + "&nbsp;<i class='icon-caret-down'></i>";
                }

                Repeater rptSubCates = e.Item.FindControl("rptSubCates") as Repeater;
                if (rptSubCates != null && cate.SubCategories.Count > 0)
                {
                    rptSubCates.DataSource = cate.SubCategories.ToList();
                    rptSubCates.DataBind();
                }
            }
        }
    }
}